package com.shiwaixiangcun.customer.module.intelligent

import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.tencent.smtt.sdk.*
import kotlinx.android.synthetic.main.activity_camera_off_reason.*
import kotlinx.android.synthetic.main.top_bar_white.*

/**
 * 智能设备离线原因
 */
class SmartCameraOffReasonActivity : BaseActivity() {

    var strDeviceName: String = ""
    var offReasonUrl: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera_off_reason)
        initBundleData()
        initView()
    }

    private fun initBundleData() {
        strDeviceName = intent.extras.getString("device_type")


    }

    private fun initView() {

        if (strDeviceName.equals("watch")) {
            tv_page_name.text = "智能手表离线可能原因"
            offReasonUrl = GlobalApi.watchOffReason.toString()
        } else {
            tv_page_name.text = "智能摄像机离线可能原因"
            offReasonUrl = GlobalApi.cameraOffReason.toString()
        }

        back_left.setOnClickListener { finish() }
        removeCookie(this)
        webView_reason.clearCache(true)
        removeCookie(this)
        webView_reason.webChromeClient = MyWebChromeViewClient()
        webView_reason.webViewClient = MyWebViewClient()

        webView_reason.loadUrl(offReasonUrl)

    }

    /**
     * 清除Cookie
     *
     * @param context
     */
    private fun removeCookie(context: Context) {
        CookieSyncManager.createInstance(context)
        val cookieManager = CookieManager.getInstance()
        cookieManager.removeAllCookie()
        CookieSyncManager.getInstance().sync()
    }



    override fun onDestroy() {
        if (webView_reason != null) {
            webView_reason.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
            webView_reason.clearHistory()

            (webView_reason.parent as ViewGroup).removeView(webView_reason)
            webView_reason.destroy()
        }
        super.onDestroy()
    }

    private inner class MyWebChromeViewClient : WebChromeClient() {

        override fun onProgressChanged(view: WebView, newProgress: Int) {
            if (newProgress == 100) {
                myProgressBar.visibility = View.GONE
            } else {
                if (View.INVISIBLE == myProgressBar.visibility) {
                    myProgressBar.visibility = View.VISIBLE
                }
                myProgressBar.progress = newProgress
            }
            super.onProgressChanged(view, newProgress)
        }


    }

    /**
     * 处理通知 ，请求事件
     */
    private inner class MyWebViewClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            view.loadUrl(url)
            return true
        }
    }

}
